package com.cos.openapi.web.controller;

import com.cos.openapi.common.Const;
import com.cos.openapi.entity.AdminUser;
import com.cos.openapi.entity.Menu;
import com.cos.openapi.pojo.JsonResult;
import com.cos.openapi.services.AdminUserServices;
import com.cos.openapi.services.MenuServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.PseudoColumnUsage;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: COS
 * @time: 2021/1/29 9:01
 * @description:
 */
@Controller
public class SysController {
    @Autowired
    private AdminUserServices adminUserServices;

    @Autowired
   private MenuServices menuServices;
    /**
     *
     * @author: COS
     * @return:
     * @time: 2021/1/29 9:02
     * @description: 注销的方法
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

    @RequestMapping("/dologin")
    @ResponseBody
    public JsonResult dologin(String email ,String password,HttpSession session){
        JsonResult jsonResult = adminUserServices.doLogin(email, password);
        session.setAttribute(Const.ISLOGIN, jsonResult.getResult());
        return jsonResult;
    }

    @RequestMapping("/side")
    @ResponseBody
    public JsonResult side(HttpSession session){
        AdminUser adminUser = (AdminUser) session.getAttribute(Const.ISLOGIN);
        if (adminUser == null) {
            //没有登录就给个空集合
            return JsonResult.getJsonResultByFail("请求先登录",new ArrayList<>());
        }
        //已经登录了
        //获取权限菜单数据
        //用户主键id
        List<Menu> menuTree = menuServices.getMenuTree(adminUser.getId());
        return JsonResult.getJsonResultBySuccess(menuTree);
    }

}
